<script lang="ts" setup>
import { ref, watch, reactive, Ref } from "vue";

const emit = defineEmits<{
  (e: "update:visible", val: boolean): void;
  (e: "save-success", val: any[]): void; //新增保存 修改保存 成功回调
}>();

const props = defineProps({
  //是否显示
  visible: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => {}
  }
});

//设置是否显示双向绑定
const visibleModelValue = ref(props.visible);
watch(
  () => props.visible,
  val => {
    visibleModelValue.value = val;
    if (val && props.data) {
      AppMenuFormNewData.value = props.data;
    }
  }
);
watch(visibleModelValue, val => {
  emit("update:visible", val);
});

const AppMenuFormNewData: Ref<any> = ref({});

const ruleFormRef = ref();

const appMenuTreeNewDialogState = reactive({
  rules: {
    name: [{ required: true, message: "请输入按钮后缀名称", trigger: "blur" }],
    code: [{ required: true, message: "请输入按钮后缀编码", trigger: "blur" }]
  },
  //关闭事件
  handleClose: () => {
    visibleModelValue.value = false;
  },
  //触发表单保存
  onOkButtonClick: () => {
    ruleFormRef.value.validate((valid: boolean) => {
      if (valid) {
        emit("save-success", AppMenuFormNewData.value);
      }
    });
  }
});
</script>
<template>
  <div class="jxtech-dialog-form">
    <el-dialog
      v-model="visibleModelValue"
      title="填写信息"
      width="40%"
      destroy-on-close
      :before-close="appMenuTreeNewDialogState.handleClose"
    >
      <el-form ref="ruleFormRef" :model="AppMenuFormNewData" :rules="appMenuTreeNewDialogState.rules" label-width="120px">
        <el-row>
          <el-col :span="16" :offset="3">
            <el-row :gutter="24">
              <el-col :span="24">
                <el-form-item label="按钮名称" prop="name">
                  <el-input v-model="AppMenuFormNewData.name" placeholder="请输入按钮后缀名称" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="24">
                <el-form-item label="按钮编码" prop="code">
                  <el-input v-model="AppMenuFormNewData.code" placeholder="请输入按钮后缀编码" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <jxtech-cancel-button @click="visibleModelValue = false"></jxtech-cancel-button>
        <jxtech-ok-button @click="appMenuTreeNewDialogState.onOkButtonClick"></jxtech-ok-button>
      </template>
    </el-dialog>
  </div>
</template>
